package com.hnu.web;

import org.apache.struts2.ServletActionContext;

import com.hnu.model.Bookmark;
import com.hnu.model.User;
import com.hnu.model.Userbookmark;
import com.hnu.util.PageInfoBean;

public class UserBookmarkBean extends BaseAction {
	private static final long serialVersionUID = -7653767330124165856L;
	private int pageSize = 10;
	private int pageIndex = 1;
	private PageInfoBean pageInfo;
	private long helpUserBookmarkId;
	private Userbookmark userbookmark;
	private Bookmark bookmark;

	private String message = "";

	public String pageUserBookmark() {
		User user = (User) this.session.get("user");
		long userId = user.getUserId();
		int allCount = this.servLocator.getBookmarkServ()
				.getUserBookmarkCountByUserId(userId);
		int pages = (int) Math.ceil(((double) allCount) / pageSize);

		if (this.pageIndex > pages) {
			this.pageIndex = pages;
		}

		if (this.pageIndex < 1) {
			this.pageIndex = 1;
		}

		this.pageInfo = this.servLocator.getBookmarkServ()
				.getUserBookmarkByUserId(userId, this.pageSize, this.pageIndex);
		this.pageInfo.setPageNum(pages);
		this.pageInfo.setTotal(allCount);
		return "success";
	}

	public String deleteUserBookmark() {
		User user = (User) this.session.get("user");
		boolean isCanModify = this.servLocator.getBookmarkServ().isCanModify(
				this.helpUserBookmarkId, user.getUserId());
		if (isCanModify) {
			this.servLocator.getBookmarkServ().deleteUserBookmark(
					this.helpUserBookmarkId);
		}
		return this.pageUserBookmark();
	}

	public String toEditUserBookmark() {
		User user = (User) this.session.get("user");
		boolean isCanModify = this.servLocator.getBookmarkServ().isCanModify(
				this.helpUserBookmarkId, user.getUserId());
		if (!isCanModify) {
			return "failure";
		}
		this.userbookmark = this.servLocator.getBookmarkServ()
				.getUserBookmarkByID(this.helpUserBookmarkId);
		return "success";
	}

	public String editUserBookmark() {
		if (this.userbookmark.getBookmarkTitle() == null
				|| this.userbookmark.getBookmarkTitle().trim().isEmpty()
				|| this.userbookmark.getBookmarkDesc() == null
				|| this.userbookmark.getBookmarkDesc().trim().isEmpty()) {
			this.message = "所有字段都不允许为空!";
			return "failure";

		} else {
			this.servLocator.getBookmarkServ().updateUserBookmark(
					this.userbookmark);
			return this.pageUserBookmark();
		}
	}

	public String addUserBookmark() {
		if (this.bookmark.getBookmarkTitle() == null
				|| this.bookmark.getBookmarkTitle().trim().isEmpty()
				|| this.bookmark.getBookmarkDesc() == null
				|| this.bookmark.getBookmarkDesc().trim().isEmpty()
				|| this.bookmark.getBookmarkUrl() == null
				|| this.bookmark.getBookmarkUrl().trim().isEmpty()) {
			this.message = "所有字段都不允许为空!";
			return "failure";

		} else {
			User user = (User) this.session.get("user");
			long userId = user.getUserId();
			String indexPath = ServletActionContext.getRequest().getRealPath(
					"/")
					+ "indexPath";
			if (this.servLocator.getBookmarkServ().addBookmark(userId,
					bookmark, indexPath)) {
				return this.pageUserBookmark();
			}
			else
			{
				this.message = "您已收录该书签!";
				return "failure";
			}
		}
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public PageInfoBean getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfoBean pageInfo) {
		this.pageInfo = pageInfo;
	}

	public long getHelpUserBookmarkId() {
		return helpUserBookmarkId;
	}

	public void setHelpUserBookmarkId(long helpUserBookmarkId) {
		this.helpUserBookmarkId = helpUserBookmarkId;
	}

	public void setUserbookmark(Userbookmark userbookmark) {
		this.userbookmark = userbookmark;
	}

	public Userbookmark getUserbookmark() {
		return userbookmark;
	}

	public Bookmark getBookmark() {
		return bookmark;
	}

	public void setBookmark(Bookmark bookmark) {
		this.bookmark = bookmark;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}
